import java.util.Stack;

public class Do_0 {
    public static void main(String[] args) {
        Herocode1 herocode=new Herocode1("宋江",1,"及时雨");
        Herocode1 herocode1=new Herocode1("吴用",3,"智多星");
        Herocode1 herocode2=new Herocode1("林冲",2,"豹子头");
        SingleLinkeLinkedst singleLinkeLinkedst=new SingleLinkeLinkedst();
        singleLinkeLinkedst.addByOrder(herocode);
        singleLinkeLinkedst.addByOrder(herocode1);
        singleLinkeLinkedst.addByOrder(herocode2);
        singleLinkeLinkedst.showHero();

        singleLinkeLinkedst.delHeroNode(1);
        singleLinkeLinkedst.showHero();
       /* Herocode1 herocode4=new Herocode1("公孙胜",3,"入云龙");
        singleLinkeLinkedst.UpdateHeroNode(herocode4);
        singleLinkeLinkedst.showHero();
*/
    }
}


class Herocode1{
    public String name;
    public int no;
    public String nickname;
    public Herocode1 pro;
    public Herocode1 next;

    public Herocode1(String name, int no, String nickname) {
        this.name = name;
        this.no = no;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "Herocode1{" +
                "name='" + name + '\'' +
                ", no=" + no +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
class SingleLinkeLinkedst{
    private Herocode1 herocode=new Herocode1("",0,"");

    public Herocode1 getHerocode(){
        return herocode;
    }


    public void add(Herocode1 herocode1){
        Herocode1 temp=herocode;
        while (true){
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        temp.next =herocode1;
        herocode1.pro=temp;
    }
    public  void reversePrint(Herocode1 herocode1){
        if (herocode.next==null){
            System.out.println("链表为空");
            return;
        }
        Stack<Herocode1> stack=new Stack<>();
        Herocode1 temp=herocode.next;
        while (temp!=null){
            stack.push(temp);
            temp=temp.next;
        }
        while (stack.size()>0){
            System.out.println(stack.pop());
        }
    }


    public void UpdateHeroNode(Herocode1 NewHercode){

        boolean flg=false;
        if (herocode.next==null){
            System.out.println("链表为空");
            return;
        }
        Herocode1 temp=herocode.next;
        while (true){
            if (temp.no==NewHercode.no){
                flg=true;//找到
                break;
            }
            if (temp.next==null) {
                break;//已经遍历完
            }
            temp=temp.next;
        }
        if (flg){
            temp.name=NewHercode.name;
            temp.nickname= NewHercode.nickname;
        }else {
            System.out.println("没有找到"+NewHercode.no);
        }
    }


    public void delHeroNode(int no){
        Herocode1 temp=herocode;
        boolean blg=false;
        if (herocode.next==null){
            System.out.println("链表为空");
            return;
        }
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.no==no){
                blg=true;
                break;
            }
            temp=temp.next;
        }
        if (blg){
            temp.pro.next=temp.next;
            temp.next.pro=temp.pro;
        }else {
            System.out.println("没有找到"+no);
        }
    }

    public void addByOrder(Herocode1 herocode1){
        Herocode1 temp=herocode;
        boolean flg=false;
        while (true){
            if (temp.next==null){
                break;//temp已经在表最后
            }
            if (temp.next.no>herocode1.no){//位置找到，在temp后面插入
                break;
            }
            if (temp.next.no==herocode1.no){
                flg=true;//说明新点已经存在
                break;
            }
            temp=temp.next;
        }
        if (flg){
            System.out.println("新点已经存在");
        }else {
            herocode1.next=temp.next;
            if (temp.next!=null){
            temp.next.pro=herocode1;}
            herocode1.pro=temp;
            temp.next=herocode1;




        }

    }
    public void showHero(){
        if (herocode.next==null){
            System.out.println("链表为空");
            return;
        }
        Herocode1 temp=herocode.next;
        while (true){
            if (temp==null){
                break;
            }
            System.out.println(temp);
            temp=temp.next;
        }
    }
}